<?php

namespace app\api\controller;

use app\common\controller\Utils;
use app\common\controller\Wxcode;
use app\common\controller\Wechat;
use app\admin\model\Quarters;
use app\admin\model\User;
use app\admin\model\Register;
use MongoDB\Driver\ReadConcern;
use think\Cache;
use think\Controller;
use think\Db;

/**
 * 首页接口
 */
class Index extends Controller
{

    public function clear(){
        Cache::clear();
    }

    /**
     * 检验token是否过期
     */
    public function Cachetoken(){

        if(!$this->request->header('token')){
            echo json_encode(["code"=>300,"msg"=>'授权失败,请退出重试!',"data"=>null]);
            die;
        }else{
            //获取缓存内容
            $uid = Cache::get($this->request->header('token'));
            //清除缓存
            Cache::clear($this->request->header('token'));
            //重新缓存
            Cache::set($this->request->header('token'),$uid,10800);
        }

    }


    //获取小区名称
    public function getQuarters(){
        $this->Cachetoken();
        $id = $this->request->param('xqid');
        $uid = Cache::get($this->request->header('token'));
        $Usermodel = new User();
        $user = $Usermodel->get($uid);
        if (!$user){
            $data['isinfoComplete'] = 0;
            echo json_encode(["code"=>300,"msg"=>'授权失败,请退出重试!',"data"=>null]);
            die;
        }else{
            $data['isinfoComplete'] =  ($user->name&&$user->phone&&$user->housenumber)? 1 : 0 ;
        }

        $data['name'] = Quarters::where('id',$id)->value('name');
        if ($data['name']) return  Utils::sendms('200', '请求成功',$data);
        return  Utils::sendms('400', '小区不存在',null);
    }

    //微信授权
    public function wxauth(){
        $code = $this->request->param('code');
        $Wechat = new Wechat();
        $data = $Wechat->VueWxAuto($code);
        if ($data)  return  Utils::sendms('200', '请求成功',$data);
        return  Utils::sendms('300', '授权失败,请退出重试!',null);
    }

    //获取code地址
    public function setWxcodeUrl(){
        $redirect_uri = $this->request->param('redirect_uri');
        $public_appid = Db::name('systemset')->where('key','public_appid')->value('val');
        $data = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$public_appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo#wechat_redirect";
        return  Utils::sendms('200', '请求成功',$data);
    }


    //提交数据
    public function register(){
        $this->Cachetoken();
        $uid = Cache::get($this->request->header('token'));
        $co = Db::name('register')->where('uid',$uid)->whereTime('c_time','-5 minute')->count();
        if ($co) return Utils::sendms('400', '五分钟内只能提交一次!',null);
        $user = $this->request->param();
        $Usermodel = new User();
        if (!count($user)) return  Utils::sendms('400', '数据异常!',null);
        if ($user['islocal']){
            $user['islocal'] = 1;
            $user['address'] = '';
        }else{
            $user['islocal'] = 0;
            $user['address'] = $user['sourcecity'].' '.$user['address'];
        }
        if (empty($user['name'])&&empty($user['phone'])&&empty($user['housenumber'])&&empty($user['idnumber'])){
            $userres = Db::name('user')->where('id',$uid)->field('name,phone,housenumber,idnumber')->find();
            $user['name'] = $userres['name'];
            $user['phone'] = $userres['phone'];
            $user['housenumber'] = $userres['housenumber'];
            $user['idnumber'] = $userres['idnumber'];
        }
        //更新用户的数据
        $Usermodel->allowField(['name','phone','address','islocal','housenumber','idnumber'])->save($user,['id'=>$uid]);
        switch ($user['tripmode']){
            case '步行':
                $user['tripmode'] = 0;
                break;
            case '自行车':
                $user['tripmode'] = 1;
                break;
            case '电动车':
                $user['tripmode'] = 2;
                break;
            case '汽车':
                $user['tripmode'] = 3;
                break;
            case '其他':
                $user['tripmode'] = 4;
                break;
        }

        $registerinfo = [
            'tripmode'=>$user['tripmode'],
            'inout'=>$user['inout'],
            'temperature'=>$user['temperature'],
            'symptomstr'=>$user['symptomstr'],
            'uid'=>$uid,
            'carnumber'=>$user['carnumber'],
            'quarters_id'=>$user['xqid']
        ];
        $Registermodel = new Register();
        $res = $Registermodel->save($registerinfo);
        if ($res) return  Utils::sendms('200', '提交成功!',null);
        return  Utils::sendms('400', '提交失败!',null);
    }


    //获取用户数据
    public function getInfo(){
        $this->Cachetoken();
        $uid = Cache::get($this->request->header('token'));
        $data = Db::name('user')->where('id',$uid)->field('name,phone,housenumber,idnumber')->find();
        if ($data) return  Utils::sendms('200', '请求成功!',$data);
        return  Utils::sendms('400', '获取失败!',null);
    }

    //提交用户数据
    public function postInfo(){
        $this->Cachetoken();
        $uid = Cache::get($this->request->header('token'));
        $user = $this->request->param();
        $Usermodel = new User();
        $res  = $Usermodel->allowField(['name','phone','housenumber','idnumber'])->save($user,['id'=>$uid]);
        if ($res) return  Utils::sendms('200', '修改成功!',null);
        return  Utils::sendms('400', '修改失败!',null);
    }


    //进出小区记录
    public function accessRecord(){
        $this->Cachetoken();
        $uid = Cache::get($this->request->header('token'));
        $active = $this->request->param('active');

        //全部
        if ($active==0){
            $list = Db::name('register')->where('uid',$uid)->field('inout,c_time,temperature')->order('id desc')->select();

        }else{
            $list = Db::name('register')->where('uid',$uid)->where('inout',$active)->order('id desc')->field('inout,c_time,temperature')->select();
        }
        foreach ($list as $k=>$v){
            $list[$k]['c_time'] = date('Y-m-d H:i:s',$v['c_time']);
            $list[$k]['inout'] = $list[$k]['inout']==1?'进入登记':'出门登记';
        }
        return Utils::sendms('200', '修改成功!',$list);
    }

}
